from django.shortcuts import render
from django import http
# Create your views here.

# 默认页面就一个测试使用
from rest_framework.response import Response

from remote.models import BookInfo
from utils.sqlhelper import dbHelper


def index2(request):
    return render(request, 'test.html')

# 默认页面就一个测试使用
def index3(request):
    return render(request, 'index.html')

def index(request):
    return render(request,"app/index.html")   ## 返回页面

def index1(request):
    return http.HttpResponse('hahhaha') ## 返回字符串


def index4(request):
    queryset=BookInfo.objects.all()
    data=[]
    for i in queryset:
        obj={
            'name':i.name,
            'age':i.age
        }
        data.append(obj)
    return http.HttpResponse(data)

def books(request):
    books= BookInfo.objects.all()
    context={
        "books":books
    }
    return render(request,'app/books.html',context=context)


from rest_framework.views import APIView

class sqlquery(APIView):

    def post(self,request,*args,**kwargs):

        print(request.data)

        sql = """
                        select id,name,age from remote_bookinfo where remote_bookinfo.id = 1
                    """
        result = dbHelper.show2table(sql)
        print(result)

        return Response(result)